package com.hxkj.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.RankingManagementDao;
import com.hxkj.domain.po.RankingManagement;
import com.hxkj.service.RankingManagementService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 网站资源实现类
 * <p>
 * Created by yaosy on 2018年6月8日
 */
@Service
@Slf4j
public class RankingManagementServiceImpl implements RankingManagementService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RankingManagementServiceImpl.class);

    @Autowired
    private RankingManagementDao rankingManagementDao;


    @Override
    public ResultMessageStruct save(RankingManagement rankingManagement) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try{
           //判断标题是否重复
            RankingManagement rankingManagement1 = new RankingManagement();
            rankingManagement1.setTitle(rankingManagement.getTitle());
            List<RankingManagement> rankingManagementList =  rankingManagementDao.get(rankingManagement1);
            if(rankingManagementList != null && rankingManagementList.size() > 0){

            }
            rankingManagementDao.save(rankingManagement);
            resultMessageStruct.setCode(null);
        }catch(Exception e){
            e.printStackTrace();
        }
        return resultMessageStruct;
    }

    @Override
    public PageInfo<RankingManagement> getlist(RankingManagement rankingManagement, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        PageInfo<RankingManagement> pageInfo = null;
        Page<RankingManagement> page = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<RankingManagement> rankingManagementList = new ArrayList<RankingManagement>();
        try{
            rankingManagementList = rankingManagementDao.get(rankingManagement);
            pageInfo = new PageInfo<>(page);
            pageInfo.setList(rankingManagementList);
            return pageInfo;
        }catch(Exception e){
            e.printStackTrace();
        }
        return new PageInfo<>(page);
    }

    @Override
    public ResultDataStruct<RankingManagement> get(RankingManagement rankingManagement) throws Exception {
        ResultDataStruct<RankingManagement> resultDataStruct = new ResultDataStruct<>();
        try{
            RankingManagement rankingManagement1 =  rankingManagementDao.get(rankingManagement.getRankingId());
            resultDataStruct.setData(rankingManagement1);
            resultDataStruct.setCode(null);
        }catch(Exception e){
            e.printStackTrace();
        }
        return resultDataStruct;
    }

    @Override
    public ResultMessageStruct modify(RankingManagement rankingManagement) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try{
            RankingManagement rankingManagement1 =  rankingManagementDao.get(rankingManagement.getRankingId());
            rankingManagement1.setTitle(rankingManagement.getTitle());
            rankingManagement1.setImgUrl(rankingManagement.getImgUrl());
            rankingManagement1.setSortContentSource(rankingManagement.getSortContentSource());
            rankingManagement1.setTempletId(rankingManagement.getTempletId());
            rankingManagement1.setSort(rankingManagement.getSort());
            rankingManagement1.setTypes(rankingManagement.getTypes());
            rankingManagement1.setSortOrder(rankingManagement.getSortOrder());
            rankingManagement1.setCounts(rankingManagement.getCounts());
            rankingManagement1.setComment(rankingManagement.getComment());
            rankingManagement1.setUpdateTime(rankingManagement.getUpdateTime());
            rankingManagement1.setModifier(rankingManagement1.getModifier());
            rankingManagementDao.update(rankingManagement1);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMessageStruct;
    }

    @Override
    public ResultMessageStruct delete(RankingManagement rankingManagement) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try{
            String[] rankings =  rankingManagement.getRankingIds().split(",");
            for(int a = 0 ; a < rankings.length ; a++){
                RankingManagement rankingManagement1 =  rankingManagementDao.get(rankingManagement.getRankingId());
                rankingManagement1.setDelTag(1);
                rankingManagement1.setModifier(rankingManagement.getModifier());
                rankingManagement1.setUpdateTime(rankingManagement.getUpdateTime());
                rankingManagementDao.update(rankingManagement1);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return resultMessageStruct;
    }
}
